package servlet;

import model.Department;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;

@WebServlet("/deleteServlet.action")
public class deletePartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String id = req.getParameter("id");
        int uid = Integer.parseInt(id);
        File file = new File("C:\\Users\\FeiFei\\Desktop\\test.txt");
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        List<Department> partList = null;
        try {
            partList = (List<Department>) ois.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        partList.remove(uid);

        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write("");
        fileWriter.flush();
        fileWriter.close();

        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(partList);
        oos.close();
        fos.close();
        resp.sendRedirect(req.getContextPath() + "/partInfo.jsp");
    }
}
